package com.palm.hubo2o.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Toast;

import com.palm.hubo2o.BaseActivity;
import com.palm.hubo2o.R;
import com.palm.hubo2o.adapter.PrizeListAdapter;
import com.palm.hubo2o.model.Issue;
import com.palm.hubo2o.model.LazyResBody;
import com.palm.hubo2o.model.PastIssueParams;
import com.palm.hubo2o.network.PalmRetrofit;
import com.palm.hubo2o.network.Request;
import com.palm.hubo2o.network.Result;
import com.palm.hubo2o.util.EnumCode;
import com.palm.hubo2o.util.LazyHelper;
import com.palm.hubo2o.view.SimpleListDecoration;
import com.palm.palmutils.widget.EasyRecyclerView;
import com.palm.palmutils.widget.refreshlayout.RefreshLayout;

import butterknife.Bind;
import butterknife.BindColor;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * Created by dest1 on 2015/10/13.
 */
public class PrizeListActivity extends BaseActivity implements EasyRecyclerView.OnItemClickListener {
    @Bind(R.id.recycler_view)
    EasyRecyclerView mRecyclerView;
    @Bind(R.id.refresh_layout)
    RefreshLayout mRefreshLayout;
    @Bind(R.id.fab_prize_list)
    FloatingActionButton mFab;
    @BindColor(R.color.dividerColorDark)
    int dividerColor;
    private EnumCode.gameId lotteryId;
    private PrizeListAdapter mAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prize_list);
        lotteryId = (EnumCode.gameId) getIntent().getSerializableExtra("gameId");
        setTitle(lotteryId.getName() + "开奖列表");
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setHasFixedSize(true);
//        mRecyclerView.addItemDecoration(new SimpleListDividerDecorator(ContextCompat.getDrawable(getContext(), R.drawable.divider_horizontal_dark), true));
        mRecyclerView.addItemDecoration(new SimpleListDecoration(dividerColor, 1));
        mRecyclerView.setAdapter(mAdapter = new PrizeListAdapter());
        mAdapter.setGameId(lotteryId);
        mRecyclerView.setSelector(ContextCompat.getDrawable(this, R.drawable.selector_foreground));
        mRecyclerView.setDrawSelectorOnTop(true);
        mRecyclerView.setOnItemClickListener(this);
        mHelper.attach(mRefreshLayout, mRecyclerView, this);
        mHelper.loadFirst();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHelper.detach();
    }

    @Override
    public boolean onItemClick(EasyRecyclerView parent, View view, int position, long id) {
        if (lotteryId == EnumCode.gameId.HBP3) {
            Toast.makeText(this, "快3没有详情页", Toast.LENGTH_SHORT).show();
            return false;
        }
        Intent i = new Intent(this, PrizeDetailActivity.class);
        i.putExtra("issue", mAdapter.getIssues().get(position));
        startActivity(i);
        return true;
    }

    @OnClick(R.id.fab_prize_list)
    public void goToStake(FloatingActionButton fab) {
        switch (lotteryId) {
            case SSQ:
                startActivity(new Intent(this, SSQLotteryActivity.class));
                break;
            case QLC:
                startActivity(new Intent(this, QLCLotteryActivity.class));
                break;
            case D3:
                startActivity(new Intent(this, D3LotteryActivity.class));
                break;
            case HBP3:
                startActivity(new Intent(this, K3LotteryActivity.class));
                break;
//            case C522:
//                startActivity(new Intent(this, C225LotteryActivity.class));
//                break;
            default:
                break;
        }
        finish();
    }

    private LazyHelper<PastIssueParams, LazyResBody<Issue>> mHelper = new LazyHelper<PastIssueParams, LazyResBody<Issue>>() {
        @Override
        public void onLazyResponse(LazyResBody<Issue> lazyResBody, boolean refresh) {
            if (refresh)
                mAdapter.setIssues(lazyResBody.getList());
            else
                mAdapter.updateIssues(lazyResBody.getList());
        }

        @Override
        public Call<Result<LazyResBody<Issue>>> getLazyCall(Request<PastIssueParams> request) {
            return PalmRetrofit.getInstance().getApi().queryPastIssue(request);
        }

        @Override
        public Request<PastIssueParams> getLazyRequest() {
            return new Request<>(new PastIssueParams(lotteryId.name()));
        }
    };

}
